<?php

use yii\grid\GridView;
use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $searchModel app\models\search\AsrPlanSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '';
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="asr-plan-index">

    <p>
        <?= Html::a('新增呼叫计划', ['create'], ['class' => 'btn btn-info fa fa-plus']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['attribute' => 'planname',
                'footer' => '<input id="num"  type="number" href="#" class="btn btn-default btn-xs btn-delete" placeholder="每页显示的条数"/>',
                'options' => [
                    'width' => '15%',
                ],
            ],
            [
                'attribute' => 'created_at',
                'value' => function ($model) {
                    return date('Y-m-d H:i:s', $model->created_at);
                }
            ],

            //线路名
            [
                'attribute' => 'line_num',
                'value' => function ($model) {
                    $line_num = \app\models\AsrCall::find()->select('line,call_id')->where(['call_id' => $model->call_id])->one();
                    return $line_num['line'];
                    //var_dump(\app\models\AsrCall::find()->where(['call_id'=>$model->call_id])['listname']);
                }
            ],
            //话术名称
            [
                'attribute' => 'audio_name',
                'value' => function ($model) {
                    $audio_name = \app\models\AsrAudio::find()->select('audio_id,name')->where(['audio_id' => $model->audio_id])->one();
                    return $audio_name['name'];
                }
            ],
            //电话表
            [
                'attribute' => 'listname',
                'options' => [
                    'width' => '10%',
                ],
                'value' => function ($model) {
                    return \app\models\AsrMobile::find()->where(['mobile_id' => $model->mobile_id])->one()['listname'];
                }
            ],
            //分类名
            ['attribute' => 'name',
                'options' => [
                    'width' => '10%',
                ],
                'value' => function ($model) {
                    $name = \app\models\AsrAudio::find()->where(['audio_id' => $model->audio_id])->one()['audio_classify_id'];
                    $name = \app\models\AsrAudioClassify::findOne($name)['name'];
                    return $name;
                }

            ],
            //所属
            [
                'attribute' => 'belong',
                'options' => [
                    'width' => '10%',
                ],
                'value' => function ($model) {
                    $belong = \app\models\AsrAudio::find()->where(['audio_id' => $model->audio_id])->one();
                    //var_dump($belong);
                    if ($belong['blong'] == 1) {
                        return '自有';
                    } else {
                        return '非自有';
                    }
                }
            ],
            [
                'attribute' => 'state',
                'format'=>'raw',
                'options' => [
                    'width' => '10%',
                ],
                'value' => function ($model) {
                    if ($model->state == 0) {
                        return '<span style="color:#ff2c2f;"><b>暂停</b></span>';
                    } elseif ($model->state == 1) {
                        return '<span style="color:#00A65A;"><b>正在执行</b></span>';
                    } elseif ($model->state == 2) {
                        return '<span style="color:#005da6;"><b>执行完成</b></span>';
                    }
                }
            ],
            //执行
            [
                'class' => 'yii\grid\ActionColumn',
                'header' => '操作',
                'template' => '{disable}',
                'buttons' => [
                    'disable' => function ($url, $model, $key) {
                        if ($model->state == 1) {
                            return "<span class='span'><a href='/asr-customer/view?id=$model->mobile_id' title='查看' aria-label='查看' data-pjax='0' class='btn btn-success fa fa-sticky-note-o'>详情</a>&nbsp;&nbsp;<a id='$url' audio_id=$key   class='btn btn-danger fa fa_edit pause play'><span class='glyphicon glyphicon-pause'></span>暂停</a></span>";
                        } else if ($model->state == 0) {

                            return "<span class='span'><a href='/asr-customer/view?id=$model->mobile_id' title='查看' aria-label='查看' data-pjax='0' class='btn btn-success fa fa-sticky-note-o'>详情</a>&nbsp;&nbsp;<a href='/asr-plan/update?id=$key' title='更新' aria-label='更新' data-pjax='0' class='btn btn-info fa fa-edit'>修改</a>&nbsp;&nbsp;<a href='/asr-plan/delete?id=$key' title='删除' aria-label='删除' data-pjax='0' data-confirm='您确定要删除此项吗？' data-method='post' class='btn btn-danger fa fa-remove'><span class='glyphicon glyphicon-trash' ></span></a>&nbsp;&nbsp;<a id='$url' audio_id=$key class='btn btn-success play'><span class='glyphicon glyphicon-play'></span>执行</a></span> ";
                        }

                    },
                ],
                'headerOptions' => ['width' => '350'],
            ],
        ],
        'filterModel' => $searchModel,
        'showFooter' => true,  //设置显示最下面的footer
        'pager' => [
            'firstPageLabel' => "第一页",
            'lastPageLabel' => '最后一页',
        ],
    ]); ?>
</div>

<script>
    $(function () {
        $(".play").on('click',function() {
            var url = this.id;
            $.ajax({
                type:'get',
                url:url,
                success: function (datas) {
                    if(datas == "success"){
                        location.reload();
                        alert('执行成功！');
                    }else{
                        alert('执行失败！');
                    }
                },
                error: function (error) {
                }
            });

        });


        var Default = {
            animationSpeed: 500,
        }
        //自定义分页
        $("#num").blur(function () {//当鼠标失去焦点时就出发事件，实现分页的页改变
            var url = "index";
            //alert(url);
            $.ajax({
                type: "post",
                async: true,
                cache: false,
                url: url, /*url写异域的请求地址*/
                data: {
                    pageSize: $("#num").val()
                },
                success: function (datas) {
                    window.location.reload()
                },
                error: function (error) {
                }
            });
        });
    });
</script>
